<template>
	<section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
		<!--列表-->
		<el-table :data="System" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" @row-dblclick="show">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" label="编号" width="300">
			</el-table-column>
      <el-table-column  prop="name" label="数据字典名" width="350"  sortable>
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="300" sortable>
      </el-table-column>
      <el-table-column label="操作" width="400">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

		<!--编辑界面-->
		<el-dialog :title= "title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-form-item label="数据字典名" prop="name" label-width="100px">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="sn" label-width="100px">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
    </el-dialog>

<!--    抽屉-->
    <el-drawer
        title="数据字典明细"
        :visible.sync="drawer"
        direction="rtl"
        size="50%">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true">
          <el-form-item>
            <el-button type="primary" @click="detailAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-table :data="detail" v-loading="listLoading" @selection-change="selsChange">
        <el-table-column type="index" label="编号" width="240">
        </el-table-column>
        <el-table-column property="name" label="明细名" width="200"></el-table-column>
        <el-table-column label="操作" width="400">
          <template scope="scope">
            <el-button size="small" @click="handleDetail(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="detailDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
          <el-pagination layout="prev, pager, next" @current-change="pagination"  :current-page="detailPage" :page-size="detailPageSize" :total="detailtotal" style="float:right;">
          </el-pagination>
        </el-col>
      <!--编辑界面-->
      <el-dialog :title= "title" :visible.sync="detailFormVisible" :close-on-click-modal="false" center append-to-body>
        <el-form :model="detailForm" label-width="80px" ref="detailForm">
          <el-form-item label="明细名" prop="name" label-width="100px">
            <el-input v-model="detailForm.name" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="detailFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="detailSubmit" :loading="detailLoading">提交</el-button>
        </div>
      </el-dialog>
    </el-drawer>

	</section>
</template>

<script>

	export default {

		data() {
			return {
				keyword: '',
        System:[],
        detail:[],
        options:[],
        detailtotal:0,
        detailPage:1,
        detailPageSize:5,
				total: 0,
				page: 1,
        pageSize: 5,
				listLoading: false,
        drawer: false,
        title:"",
				sels: [],//列表选中列
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
        detailFormVisible: false,//编辑界面是否显示
        detailLoading: false,
				//编辑界面数据
				editForm: {
					id: 0,
          sn:"",
					name: '',
				},
        //明细编辑界面
        detailForm:{
          name: '',
          types_id:''
        },
        typeid:null,
			}
		},
		methods: {
      handleCurrentChange(val) {
        this.page = val;
        this.getSystem();

      },
      //明细分页
      pagination(val){
        this.detailPage = val;
        this.show2();
      },
      //获取用户列表
      getSystem() {
        let para = {
          currentPage: this.page,
          pageSize: this.pageSize
        };
        this.listLoading = true;
        this.$http.post("/system/pageList", para).then(res => {
          this.total = res.data.total;
          this.System = res.data.data;
          this.listLoading = false;
        })
      },
      //删除
      handleDel(row) {
        //1.删除的确认框
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {//确认删除的按钮
          this.listLoading = true;
          this.$http.delete("/system/systemdictionary/" + row.id).then(res => {
            this.listLoading = false;
            if (res.data.success) {
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
            } else {
              this.$message({
                message: res.data.msg,
                type: 'error'
              });
            }
            //重新加载数据
            this.getSystem();
          })
        }).catch(() => {//取消删除回调函数
          this.$message('已取消删除');
        })
      },
      //明细删除
      detailDel: function(row) {
        //1.删除的确认框
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {//确认删除的按钮
          this.listLoading = true;
          this.$http.delete("/detail/detail/" + row.id).then(res => {
            this.listLoading = false;
            if (res.data.success) {
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
            } else {
              this.$message({
                message: res.data.msg,
                type: 'error'
              });
            }
            //重新加载数据
            this.show(this.typeid);
          })
        }).catch(() => {//取消删除回调函数
          this.$message('已取消删除');
        })
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        //重置表单
        this.resetForms()
        this.title = "编辑",
        this.editFormVisible = true;
        this.editForm = Object.assign({}, row);
      },
      //明细显示编辑界面
      handleDetail: function (index, row) {
        //重置表单
        this.detailResetForms()
        this.title = "编辑",
        this.detailFormVisible = true;
        this.detailForm = Object.assign({}, row);
      },
      //显示新增界面
      handleAdd: function () {
        //重置表单
        this.resetForms()
        this.title = "新增",
        this.editFormVisible = true;
        this.editForm = {
          name: '',
          sn:"",
        };
      },
      //明细显示新增界面
      detailAdd: function () {
        //重置表单
        this.detailResetForms()
        this.title = "新增",
        this.detailFormVisible = true;
        this.detailForm = {
          name: '',
        };
      },
      show:function (row){
        if(row.id){
          this.typeid=row.id
        }
       this.drawer = true
        this.detailPage = 1

       this.show2()
      },
      show2:function (){
        let para = {
          currentPage: this.detailPage,
          pageSize: this.detailPageSize,
          keyword :this.typeid
        };
        console.log(para)
        this.listLoading = true;
        this.$http.post("/detail/pageList", para).then(res =>{
          this.detailtotal = res.data.total;
          this.detail = res.data.data;
          this.listLoading = false;
        })
      },
      //编辑
      editSubmit: function () {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              let para = Object.assign({}, this.editForm);
              this.$http.put("/system/systemdictionary",para).then(res =>{
                  this.editLoading = false;
                if (res.data.success) {
                  this.$message({
                    message: res.data.msg,
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: res.data.msg,
                    type: 'error'
                  });
                }
                //重新加载数据
                this.getSystem();
              })
            });
            this.editFormVisible = false;
          }
        });
      },

      //明细编辑
      detailSubmit: function (row) {
        this.$refs.detailForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.detailLoading = true;
              let para = Object.assign({}, this.detailForm);
              para.types_id=this.typeid
              console.log(para)
              this.$http.put("/detail/detail",para).then(res =>{
                this.detailLoading = false;
                if (res.data.success) {
                  this.$message({
                    message: res.data.msg,
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: res.data.msg,
                    type: 'error'
                  });
                }
                //重新加载数 据
                this.show(row);
              })
            });
            this.detailFormVisible = false;
          }
        });
      },
      selsChange: function (sels) {
        this.sels = sels;
      },
    resetForms: function (){
       if(this.$refs['editForm']){
       this.$refs['editForm'].resetFields();//重置表单验证
        }
      },
      detailResetForms: function (){
        if(this.$refs['detailForm']){
          this.$refs['detailForm'].resetFields();//重置表单验证
        }
      },
  },

		mounted() {
			this.getSystem();
		}
	}

</script>

<style scoped>

</style>